home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / support / xexemon.c < prev   
Encoding:
C/C++ Source or Header  |  1992-08-27  |  8.8 KB  |  429 lines

  1. /* ----------------------------------------------------------------
  2.  *   FILE
  3.  *    xexemon.c    postgres executor monitoring program
  4.  *
  5.  *   DESCRIPTION
  6.  *    this is a simple interface to Doug Young's tree widget.
  7.  *    The program creates a viewport widget containing a tree
  8.  *    widget and then uses XAddInput() to direct input from
  9.  *    stdin to input_callback(), a routine which collects the
  10.  *    input and passes it to process_input, which does all
  11.  *    the "real" work.
  12.  *
  13.  *   NOTES
  14.  *    This code depends on the X11 athena toolkit.
  15.  *
  16.  *   IDENTIFICATION
  17.  *    $Header: /private/postgres/src/support/RCS/xexemon.c,v 1.1 1990/08/26 16:48:17 cimarron Exp $
  18.  * ----------------------------------------------------------------
  19.  */
  20. #include <stdio.h>
  21. #include <X11/Xlib.h>
  22. #include <X11/Intrinsic.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Xaw/Viewport.h>
  25. #include <X11/Xaw/Label.h>
  26. #include "Tree.h"    
  27.  
  28. /* ----------------
  29.  *    global variables
  30.  * ----------------
  31.  */
  32. Widget  toplevel;
  33. Widget  viewport;
  34. Widget  tree;
  35. Widget  root;
  36.  
  37. Arg     args[10];
  38.  
  39. /* ----------------
  40.  *    problem    - subroutine to print an error message
  41.  * ----------------
  42.  */
  43. void problem(m1, m2)
  44.     char *m1;
  45.     char *m2;
  46. {
  47.     fprintf(stderr, "%s [%s]\n", m1, m2);
  48. }
  49.  
  50. /* ----------------
  51.  *    strmake - subroutine to make a copy of a string
  52.  * ----------------
  53.  */
  54. char  *strmake(str, len)
  55.     char *str;
  56.     int len;
  57. {
  58.     char *newstr;
  59.     if (str == NULL) return NULL;
  60.     if (len <= 0) len = strlen(str);
  61.     
  62.     newstr = (char *) malloc((unsigned) len+1);
  63.     (void) strncpy(newstr, str, len);
  64.     newstr[len] = (char) 0;
  65.     return newstr;
  66. }
  67.  
  68. /* ----------------
  69.  *    pindex - subroutine to find the position of a char in a string
  70.  * ----------------
  71.  */
  72. int pindex(s, c)
  73.     char *s;
  74.     char c;
  75. {
  76.     char *p = (char *) index(s, c);
  77.     
  78.     if (p == NULL)
  79.        return -1;
  80.  
  81.     return p-s;
  82. }
  83.  
  84. /* ----------------
  85.  *    get_param - subroutine to pull a sequence of characters from
  86.  *            a string up to the first comma and return a pointer
  87.  *            to the first char after the comma.
  88.  * ----------------
  89.  */
  90. char *get_param(cmd, return_param)
  91.     char *cmd;
  92.     char **return_param;
  93. {
  94.     int comma;
  95.     
  96.     comma = pindex(cmd, ',');
  97.     if (comma == -1) {
  98.     (*return_param) = NULL;
  99.     return cmd;
  100.     }
  101.  
  102.     if (comma == 0) {
  103.     (*return_param) = NULL;
  104.     return cmd + 1;
  105.     }
  106.     
  107.     (*return_param) = strmake(cmd, comma);
  108.  
  109.     return (char *)
  110.     cmd + comma + 1;
  111. }
  112.  
  113.  
  114. /* ----------------
  115.  *    process_input - subroutine to take a command line, determine
  116.  *            what kind of operation is requested, pull parameters
  117.  *            from the command line via get_param() as necessary
  118.  *            and finally call do the appropriate X magic.
  119.  * ----------------
  120.  */
  121. void process_input(cmd)
  122.     char *cmd;
  123. {
  124.     char *original_cmd = cmd;
  125.  
  126.     switch (*cmd++) {
  127.     /* ----------------
  128.      *    ignore nulls
  129.      * ----------------
  130.      */
  131.     case '\0':
  132.     return;
  133.     
  134.     /* ----------------
  135.      *   Create a new tree
  136.      *
  137.      *   Format:    N;
  138.      * ----------------
  139.      */
  140.     case 'N':
  141.     if (root != NULL) {
  142.         /* ----------------
  143.          *    we only need to actually recreate the widget when
  144.          *  we already have some trees..
  145.          * ----------------
  146.          */
  147.         root = NULL;
  148.         XtDestroyWidget(tree);
  149.         tree = XtCreateManagedWidget("tree",
  150.                      XstreeWidgetClass, 
  151.                      viewport,
  152.                      NULL,
  153.                      0);
  154.     }
  155.     break;
  156.     
  157.     /* ----------------
  158.      *   Add a node to the tree:
  159.      *
  160.      *   Format:    Aname,label,super;
  161.      * ----------------
  162.      */
  163.     case 'A':
  164.     {
  165.         char *nodename;
  166.         char *supername;
  167.         char *label;
  168.         Widget supernode;
  169.  
  170.         /* ----------------
  171.          *    get the name of the new node
  172.          * ----------------
  173.          */
  174.         cmd = get_param(cmd, &nodename);
  175.         if (nodename == NULL) {
  176.         problem("node name is NULL", original_cmd);
  177.         return;
  178.         }
  179.         
  180.         /* ----------------
  181.          *    get the label of the new node
  182.          * ----------------
  183.          */
  184.         cmd = get_param(cmd, &label);
  185.  
  186.         /* ----------------
  187.          *    if this is the first node, then it becomes the root..
  188.          * ----------------
  189.          */
  190.         if (root == NULL) {
  191.         XtSetArg(args[0], XtNlabel, label);
  192.         root = XtCreateManagedWidget(nodename, labelWidgetClass,
  193.                          tree, args, 1);
  194.         free(nodename);
  195.         free(label);
  196.         return;
  197.         }
  198.  
  199.         /* ----------------
  200.          *    get the name of the node's supernode and
  201.          *  find that node in the tree
  202.          * ----------------
  203.          */
  204.         supername = cmd; 
  205.         supernode = XtNameToWidget(tree, supername);
  206.         if (supernode == NULL) {
  207.         problem("super does not exist", original_cmd);
  208.         free(nodename);
  209.         free(label);
  210.         return;
  211.         }
  212.  
  213.         /* ----------------
  214.          *    create a new widget "beneath" the supernode
  215.          *  and return.
  216.          * ----------------
  217.          */
  218.         XtSetArg(args[0], XtNsuperNode, supernode);
  219.         XtSetArg(args[1], XtNlabel, label);
  220.         XtCreateManagedWidget(nodename, labelWidgetClass,
  221.                   tree, args, 2);
  222.         free(nodename);
  223.         free(label);
  224.         return;
  225.     }
  226.     break;
  227.     
  228.     /* ----------------
  229.      *   Send a signal to a node
  230.      *
  231.      *   Format:    Sname,code,parameters...;
  232.      * ----------------
  233.      */
  234.     case 'S':
  235.     {
  236.         char *nodename;
  237.         char *message;
  238.         Widget node;
  239.  
  240.         /* ----------------
  241.          *    get the name of the new node
  242.          * ----------------
  243.          */
  244.         cmd = get_param(cmd, &nodename);
  245.         if (nodename == NULL) {
  246.         problem("node name is NULL", original_cmd);
  247.         return;
  248.         }
  249.         node = XtNameToWidget(tree, nodename);
  250.         if (node == NULL) {
  251.         problem("node not found", original_cmd);
  252.         return;
  253.         }
  254.  
  255.         /* ----------------
  256.          *    get the signal code
  257.          * ----------------
  258.          */
  259.         cmd = get_param(cmd, &message);
  260.         switch (*message) {
  261.         /* ----------------
  262.          *    T -- update node text
  263.          * ----------------
  264.          */
  265.         case 'T':
  266.         {
  267.             XtSetArg(args[0], XtNlabel, cmd);
  268.             XtSetValues(node, args, 1);
  269.             break;
  270.         }
  271.         /* ----------------
  272.          *    B -- set the border width
  273.          * ----------------
  274.          */
  275.         case 'B':
  276.         {
  277.             int width = atoi(cmd);
  278.             XtSetArg(args[0], XtNborderWidth, width);
  279.             XtSetValues(node, args, 1);
  280.         }
  281.         break;
  282.         default:
  283.         problem("signal code not understood", original_cmd);
  284.         break;
  285.         }
  286.         
  287.         free(message);
  288.         free(nodename);
  289.         return;
  290.     }
  291.    
  292.     /* ----------------
  293.      *   Map the top level widget
  294.      *
  295.      *   Format:    M;
  296.      * ----------------
  297.      */
  298.     case 'M':
  299.     XtMapWidget(toplevel);
  300.     return;
  301.     
  302.     /* ----------------
  303.      *   Unmap the top level widget
  304.      *
  305.      *   Format:    U;
  306.      * ----------------
  307.      */
  308.     case 'U':
  309.     XtUnmapWidget(toplevel);
  310.     return;
  311.  
  312.     /* ----------------
  313.      *   Quit the process
  314.      *
  315.      *   Format:    Q;
  316.      * ----------------
  317.      */
  318.     case 'Q':
  319.     exit(0);
  320.     
  321.     default:
  322.     problem("protocol error: ", cmd);
  323.     break;
  324.     }
  325. }
  326.  
  327. /* ----------------
  328.  *    input_callback is called whenever there is input on stdin.
  329.  *    it moves characters from the callback's input buffer to the
  330.  *    command buffer until a ';' is read.  Then the command buffer
  331.  *    is processed and collection begins again..
  332.  * ----------------
  333.  */
  334.  
  335. char    buf1[BUFSIZ];
  336. char    buf2[BUFSIZ];
  337. int     j = 0;
  338.  
  339. void input_callback(w, fd, id)
  340.     Widget         w;
  341.     int           *fd;
  342.     XtInputId      *id;
  343. {
  344.     int        nbytes;
  345.     int        i;
  346.  
  347.     nbytes = read(*fd, buf1, BUFSIZ);
  348.     
  349.     if (nbytes < BUFSIZ)
  350.     buf1[ nbytes ] = '\0';
  351.  
  352.     for (i = 0; i < nbytes; i++) {
  353.     if (buf1[i] == ';') {
  354.         buf2[j] = '\0';
  355.         process_input(buf2);
  356.         bzero(buf2, j);
  357.         j = 0;
  358.         while(buf1[++i] == '\n') /* ignore \n's right after the ; */
  359.         ;
  360.         i--;
  361.     } else
  362.         buf2[j++] = buf1[i];
  363.     }
  364. }
  365.  
  366. /* ----------------
  367.  *    main just creates the initial widgets and then enters
  368.  *    the Xt event loop
  369.  * ----------------
  370.  */
  371.  
  372. main(ac, av)
  373.     int ac;
  374.     char **av;
  375. {
  376.     /* ----------------
  377.      *    initialize the toolkit
  378.      * ----------------
  379.      */
  380.     toplevel = XtInitialize(NULL,    /* shell widget name */
  381.                 "Example",    /* application class name */
  382.                 NULL,    /* resource options */
  383.                 0,        /* number of resource options */
  384.                 &ac,    /* command line arg count */
  385.                 av);    /* command line argument list */
  386.  
  387.     /* ----------------
  388.      *     create a viewport widget to hold the tree
  389.      * ----------------
  390.      */
  391.     XtSetArg(args[0], XtNwidth,      300);
  392.     XtSetArg(args[1], XtNheight,     200);
  393.     XtSetArg(args[2], XtNallowHoriz, TRUE);
  394.     XtSetArg(args[3], XtNallowVert,  TRUE);
  395.     
  396.     viewport = XtCreateManagedWidget("viewport",
  397.                      viewportWidgetClass,
  398.                      toplevel,
  399.                      args,
  400.                      4);
  401.  
  402.     /* ----------------
  403.      *    create the initial tree widget
  404.      * ----------------
  405.      */
  406.     tree = XtCreateManagedWidget("tree",
  407.                  XstreeWidgetClass, 
  408.                  viewport,
  409.                  NULL,
  410.                  0);
  411.  
  412.     /* ----------------
  413.      *    start off with no tree..
  414.      * ----------------
  415.      */
  416.     root = (Widget) NULL;
  417.     
  418.     /* ----------------
  419.      *    add the input callback to the viewport widget and
  420.      *  realize everything.
  421.      * ----------------
  422.      */
  423.     (void) XtAddInput(fileno(stdin), XtInputReadMask,
  424.         input_callback, (caddr_t) viewport);
  425.     
  426.     XtRealizeWidget(toplevel);
  427.     XtMainLoop();
  428. }
  429.